﻿@model HotelRegistrationSystem.Models.RoomReservationModel

<table class = "roomsInfo">
    <tr>
        <td class ="name">
            @Html.DisplayNameFor(modelItem => modelItem.Rooms.First().RoomName)
        </td>
        <td class ="price">
            @Html.DisplayNameFor(modelItem => modelItem.Rooms.First().Price)
        </td>
        <td class ="roomType">
            @Html.DisplayNameFor(modelItem => modelItem.Rooms.First().RoomType)
        </td>
        <td class ="bedCount">
            @Html.DisplayNameFor(modelItem => modelItem.Rooms.First().BedCount)
        </td>
        <td id ="day0" class ="dates">
            0
        </td>
        <td id ="day1" class ="dates">
            1
        </td>
        <td id ="day2" class ="dates">
            2
        </td>
        <td id ="day3" class ="dates">
            3
        </td>
        <td id ="day4" class ="dates">
            4
        </td>
        <td id ="day5" class ="dates">
            5
        </td>
        <td id ="day6" class ="dates">
            6
        </td>
        <td class ="beds">
            Beds
        </td>
        <td class ="reserve">
            Reserve
        </td>
    </tr>

    @for (int j = 0; j < Model.Rooms.Count; j++)
    {
        if (Model.Rooms[j].RoomType.RoomTypeId == @Html.ViewBag.RoomTypeId)
        {
            <tr>
                <td>
                    @Html.HiddenFor(x => Model.Rooms[j].RoomId)
                    @if (Model.Rooms[j].RoomName != null)
                    { 
                        @Model.Rooms[j].RoomName
                        @Html.HiddenFor(x => Model.Rooms[j].RoomName)
                    }
                </td>
                <td>@Model.Rooms[j].Price лв. @Html.HiddenFor(x => Model.Rooms[j].Price)</td>
                <td>@Model.Rooms[j].RoomType.Type @Html.HiddenFor(x => Model.Rooms[j].RoomType)</td>
                <td>@Model.Rooms[j].BedCount @Html.HiddenFor(x => Model.Rooms[j].BedCount)</td>
                @if (ViewBag.SelectedDate != null)
                {
                    for (var i = 0; i < 7; i++)
                    {
                        var reserved = false;
                        var bedsReserved = 0;
                        if (Model.Rooms[j].Reservations != null)
                        {
                            for (int z = 0; z < Model.Rooms[j].Reservations.Count; z++)
                            {
                                @Html.HiddenFor(x => Model.Rooms[j].Reservations[z].Period)
                                @Html.HiddenFor(x => Model.Rooms[j].Reservations[z].ReservationStatusId)
                                @Html.HiddenFor(x => Model.Rooms[j].Reservations[z].StartDate)
                                if (Model.Rooms[j].RoomType.ReserveWhole)
                                {
                                    if (ViewBag.SelectedDate.AddDays(i).CompareTo(Model.Rooms[j].Reservations[z].StartDate) >= 0 &&
                                        ViewBag.SelectedDate.AddDays(i).CompareTo(Model.Rooms[j].Reservations[z].StartDate.AddDays(Model.Rooms[j].Reservations[z].Period)) < 0 &&
                                        Model.Rooms[j].Reservations[z].ReservationStatusId == 2)
                                    {
                                        reserved = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    if (ViewBag.SelectedDate.AddDays(i).CompareTo(Model.Rooms[j].Reservations[z].StartDate) >= 0 &&
                                        ViewBag.SelectedDate.AddDays(i).CompareTo(Model.Rooms[j].Reservations[z].StartDate.AddDays(Model.Rooms[j].Reservations[z].Period)) < 0 &&
                                        Model.Rooms[j].Reservations[z].ReservationStatusId == 2)
                                    {
                                        bedsReserved += Model.Rooms[j].Reservations[z].BedCount;
                                    }
                                }
                            }
                         }
                        if ((Model.Rooms[j].RoomType.ReserveWhole && reserved) ||
                            (!Model.Rooms[j].RoomType.ReserveWhole && Model.Rooms[j].BedCount-bedsReserved < Model.Rooms[j].SelectedBedCount))
                        {
                            <td class="roomReserved">X</td>
                        }
                        else
                        {
                            <td class="roomFree">~</td> 
                        }
                    }
                }
                <td>
                    @Html.HiddenFor(x => Model.Rooms[j].RoomTypeId)
                    @if (!Model.Rooms[j].RoomType.ReserveWhole)
                    {
                        @Html.DropDownListFor(modelItem => modelItem.Rooms[j].SelectedBedCount, new SelectList(Enumerable.Range(1, Model.Rooms[j].BedCount), Model.Rooms[j].SelectedBedCount), new Dictionary<string, object>() { { "onchange", "updateRoomsView()" } } )
                    }
                </td>
                <td>
                    @Html.CheckBoxFor(modelItem => modelItem.Rooms[j].Reserved)
                </td>
            </tr>
        }
    }
</table>
